package ${package_service_impl};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.iot.commons.ResponseResult;
import com.iot.enums.StatusEnum;
import ${package_dao}.${Table}Mapper;
import ${package_model}.${Table}Entity;
import ${package_query}.${Table}Query;
import ${package_dto}.${Table}Dto;
import ${package_vo}.${Table}Vo;
import ${package_service}.${Table}Service;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.iot.utils.ResponseResultUtil;

import java.util.List;

/**
 * ${Table}ServiceImpl
 * @author: ${author}
 * @date: ${date}
 */
@Service
@RequiredArgsConstructor
public class ${Table}ServiceImpl implements ${Table}Service {

    private final ${Table}Mapper ${table}Mapper;

    @Override
    public ResponseResult getList(${Table}Query ${table}) {
        Page<${Table}Entity> page = new Page<>(${table}.getPageIndex(),${table}.getPageSize());
        LambdaQueryWrapper<${Table}Entity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(${Table}Entity::getState, StatusEnum.VALID.getKey());
        queryWrapper.orderByDesc(${Table}Entity::getCreateTime);
        IPage<${Table}Entity> ${table}Page = ${table}Mapper.selectPage(page,queryWrapper);
        List<${Table}Vo> ${table}VoList = Lists.newArrayList();
        for (${Table}Entity ${table}Entity : ${table}Page.getRecords()) {
            ${Table}Vo vo = new ${Table}Vo();
<#list models as model>
            vo.set${model.upperName}(${table}Entity.get${model.upperName}());
</#list>
            ${table}VoList.add(vo);
        }

        return ResponseResultUtil.success(${table}Page.getTotal(),${table}VoList);
    }

    @Override
    public ResponseResult edit(${Table}Dto ${table}) {
        ${Table}Entity entity = new ${Table}Entity();
<#list models as model>
        entity.set${model.upperName}(${table}.get${model.upperName}());
</#list>
        ${table}Mapper.updateById(entity);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult add(${Table}Dto ${table}) {
        ${Table}Entity entity = new ${Table}Entity();
<#list models as model>
        entity.set${model.upperName}(${table}.get${model.upperName}());
</#list>
        ${table}Mapper.insert(entity);
        return ResponseResultUtil.success();
    }

    @Override
    public ResponseResult getById(${Table}Query ${table}) {
        ${Table}Entity ${table}Entity = ${table}Mapper.selectById(<#list models as model><#if model.id>${table}.get${model.upperName}()</#if></#list>);
        ${Table}Vo vo = new ${Table}Vo();
<#list models as model>
        vo.set${model.upperName}(${table}Entity.get${model.upperName}());
</#list>
        return ResponseResultUtil.success(vo);
    }
        
}
